/*
 * This servlet controls the status of a live lesson(or not started yet, started, stopped )
 */
package controllers.classes;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import classes.HibernateUtil;
import dao.LessonDao;
import dao.LiveLessonDao;
import models.Lesson;
import models.LiveLesson;

/**
 *
 * @author Achie
 */
public class LiveLessonStatusController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String status = request.getParameter("status");
        long lessonid = Long.parseLong(request.getParameter("lessonid"));

        HibernateUtil hu = new HibernateUtil();

        //---------------------------------------------------------------------
        //get live lesson object
        //---------------------------------------------------------------------
        LiveLessonDao lld = new LiveLessonDao(hu.openSession());
        LiveLesson liveLesson = lld.getLiveLessonFromLessonId(lessonid);
        liveLesson.setStatus(status);
        lld.update(liveLesson);

        //---------------------------------------------------------------------
        //get lesson object for redirect
        //---------------------------------------------------------------------
        LessonDao ld = new LessonDao(hu.openSession());
        Lesson lesson = ld.get(lessonid);
        
        
        hu.close();//close connection
        
        //Redirect to the lesson page
        response.sendRedirect("LectureController?courseid=" + lesson.getCourse().getId() + "&id=" + lessonid);

    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
